package top.littlejiang.onlinexam.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.littlejiang.onlinexam.common.annotation.Log;
import top.littlejiang.onlinexam.common.base.BasePage;
import top.littlejiang.onlinexam.common.utils.JWTUtils;
import top.littlejiang.onlinexam.common.utils.Result;
import top.littlejiang.onlinexam.entity.OlexamCourse;
import top.littlejiang.onlinexam.service.OlexamCourseService;

import java.io.Serializable;
import java.util.List;

/**
 * @author chen
 * @Description
 * @since 2021/3/12 18:51
 */
@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    OlexamCourseService courseService;

    /**
     * 分页查询
     * @param page
     * @return
     */
    @PostMapping("/courselist")
    @ResponseBody
    @RequiresPermissions("olexam:basis:course")
    public Result getList(@RequestBody BasePage<OlexamCourse> page){
        IPage<OlexamCourse> pages = courseService.getCourseListPage(page);
        return Result.ok(pages);
    }

    /**
     * 删除 list
     * @param ids
     * @return
     */
    @Log("禁用课程")
    @PostMapping("/del")
    @ResponseBody
    @RequiresPermissions("olexam:basis:course")
    public Result delete(@RequestBody List<Serializable> ids){
        courseService.delete(ids);
        return Result.ok();
    }

    /**
     * 保存或更新一条
     * @param course
     * @return
     */
    @Log("保存/更新班级")
    @PostMapping("/insertorupdate")
    @ResponseBody
    @RequiresPermissions("olexam:basis:course")
    public Result insertOrUpdate(@RequestBody OlexamCourse course){
        courseService.insertOrUpdate(course);
        return Result.ok();
    }

    /**
     * 返回课程 -》教师用
     * @return
     */
    @PostMapping("/byteacher")
    @ResponseBody
    @RequiresPermissions("olexam:basis:course")
    public Result selectByTeacher(){
        return Result.ok(courseService.selectByteacher(JWTUtils.getUserId()));
    }

    /**
     * 返回课程-》学生用
     * @return
     */
    @PostMapping("/byuser")
    @ResponseBody
    public Result selectByStu(){
        return Result.ok(courseService.selectByUser(JWTUtils.getUserId()));
    }

    /**
     * 根据班级找课程
     * @param courseName
     * @return
     */
    @PostMapping("/getclassbycourse")
    @ResponseBody
    public Result getClassByName(@RequestBody JSONObject courseName){
        return Result.ok(courseService.getClassByCourse(courseName.getString("courseName")));
    }
}
